package cn.com.lms.business.examRewrite.persistence.repository;

import cn.com.lms.business.examRewrite.persistence.entity.PaperQuestionReWrite;
import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface PaperQuestionReWriteDao extends BaseRepository<PaperQuestionReWrite,Long> {

    /**
     * 通过id和is_delete获取随机试卷的数据
     * @param id
     * @param is_delete
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT * FROM bnz_exam_paper_question WHERE paper_id = :id AND is_delete = :is_delete")
    List<PaperQuestionReWrite> findByIdAndIsDelete(@Param("id") Long id, @Param("is_delete") boolean is_delete);

    /**
     * 通过试卷id获取数据
     * @param id
     * @return
     */
    @Query(value = "select IFNULL(pq.single_count,0) as singleCount,IFNULL(pq.multiple_count,0) as multipleCount,IFNULL(pq.judge_count,0) as  judgeCount,IFNULL(pq.fill_count,0) as fillCount,IFNULL(pq.explain_count,0) as explainCount,q.question_name as questionName,q.id, (select count(1) from bnz_exam_question_subject qs where qs.question_id=q.id and qs.is_delete=0) as  count," +
            " (select count(1) from bnz_exam_question_subject qs where qs.type=1 and qs.question_id=q.id and qs.is_delete=0) as  totalSingleCount," +
            " (select count(1) from bnz_exam_question_subject qs where qs.type=2 and qs.question_id=q.id and qs.is_delete=0) as  totalMultipleCount," +
            " (select count(1) from bnz_exam_question_subject qs where qs.type=3 and qs.question_id=q.id and qs.is_delete=0) as  totalJudgeCount," +
            " (select count(1) from bnz_exam_question_subject qs where qs.type=4 and qs.question_id=q.id and qs.is_delete=0) as  totalFillCount," +
            " (select count(1) from bnz_exam_question_subject qs where qs.type=5 and qs.question_id=q.id and qs.is_delete=0) as  totalExplainCount from lms.bnz_exam_paper_question pq LEFT JOIN lms.bnz_exam_question q on pq.question_id=q.id where pq.is_delete=0 and  pq.paper_id=:id ", nativeQuery = true)
    List<Map> findQuestionByPaperId(@Param("id") Long id);

    /**
     * 通过PaperId删除
     * @param result
     * @param ids
     */
    @Query(nativeQuery = true,value = "UPDATE bnz_exam_paper_question SET is_delete = :result WHERE paper_id IN (:ids)")
    @Modifying
    void deletePaperQuestionReWriteByPaperId(@Param("result") boolean result, @Param("ids") List<Long> ids);
}
